#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
    int d,p;
    cin>>d>>p;
    d%=p;
    //cout<<d<<" "<<p<<endl;
    int m;
    cin>>m;
    while(m--)
    {
        int l,r;
        cin>>l>>r;
        if(p==1)
        {
            if(l>1||r==1)cout<<1<<endl;
            else cout<<2<<endl;
            continue;
        }
        if(!d)
        {
            cout<<1<<endl;
            continue;
        }
        if(r-l+1>=lcm(d, p)/d)cout<<lcm(d, p)/d<<endl;
        else cout<<r-l+1<<endl;
    }
    //cout<<lcm(0, 3);
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T=1;
    //cin>>T;
    while(T--)
    {
       solve();
    }
    return 0;
}